<template>
  <div class="ImageUpload">
    <el-upload
      v-if="!imageUrl"
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <i class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <div v-else class="myimg">
      <img :src="imageUrl" v-errorImg="defaultImg" alt="" />
      <i @click="clear" class="el-icon-close"></i>
    </div>
    <div class="myprogress">
      <el-progress v-if="isShow" :percentage="percentage" :show-text="false"></el-progress>
    </div>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
export default {
  props: ['imageUrl'],
  data() {
    return {
      cos: null,
      isShow: false,
      percentage: 0,
      defaultImg: require('@/assets/common/bigUserHeader.png')
    }
  },
  methods: {
    // 上传图片到腾讯云
    httpRequest(data) {
      // console.log(data)
      this.isShow = true
      this.cos.putObject(
        {
          Bucket: 'hr-1308447432' /* 必须 */,
          Region: 'ap-nanjing' /* 存储桶所在地域，必须字段 */,
          Key: data.file.name /* 必须 */,
          StorageClass: 'STANDARD',
          Body: data.file, // 上传文件对象
          onProgress: progressData => {
            // progressData: 上传过程中的实时进度对象
            // console.log(JSON.stringify(progressData))
            // console.log(progressData)
            this.percentage = progressData.percent * 100
          }
        },
        (err, data) => {
          // data: 上传成功后的返回值
          // err: 上传出错的错误信息
          if (err) {
            this.$message.error('上传失败')
          } else {
            this.$message.success('上传成功')
            // 上传成功后返回的图片地址  打印data获得
            // console.log(data.Location)
            setTimeout(() => {
              this.isShow = false
            }, 1000)
            // 图片显示
            this.$emit('update:imageUrl', 'http://' + data.Location)
          }
        }
      )
    },
    // 清除图片
    clear() {
      this.$emit('update:imageUrl', '')
    },
    // 上传前检查图片大小类型
    beforeAvatarUpload(file) {
      // console.log(file)
      const less2m = file.size / 1024 / 1024 < 2
      if (!less2m) {
        this.$message.error('照片大小不超过2M')
      }
      const isType = file.type === 'image/jpeg' || file.type === 'image/png'
      if (!isType) {
        this.$message.error('图片类型只能为jpeg或png')
      }
      return less2m && isType
    }
  },
  created() {
    this.cos = new COS({
      SecretId: 'AKIDXsxlJaaJlxrUrxmJZZJmytXuzoMcbIjt', // 身份识别 ID
      SecretKey: 'uXu2uUord9OM3TbT3MN9gvta091cw0nJ' // 身份密钥
    })
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #ccc;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.myimg {
  width: 180px;
  height: 170px;
  position: relative;
  img {
    width: 180px;
    height: 170px;
  }
  i {
    position: absolute;
    top: 0;
    right: 0;
    background-color: #fff;
    display: none;
  }
  &:hover i {
    display: block;
  }
}
.myprogress {
  width: 178px;
  margin: 6px 0;
}
</style>
